package com.gdth.bank.widget;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gdth.bank.R;
import com.gdth.widget.dialog.BaseDialog;

public class DialogMedia extends BaseDialog{
	private TextView txt_title;
	
	MediaPlayer mMediaMusic = null;
	
	Uri mUri;
	
	public DialogMedia(Context context, String title, boolean dismiss) {
		super(context, R.style.dialog_theme);
	
		this.title = title;
		this.dismiss = dismiss;
		
		initView();
	}
	
	@Override
	public void dismiss() {
		super.dismiss();
		mMediaMusic.stop();
	}
	
	protected void initView(){
		super.initView(R.layout.widget_menu_list);

		txt_title = (TextView) this.findViewById(R.id.dialog_title);
		txt_title.setText(title);
		LinearLayout linearlayout = (LinearLayout) findViewById(R.id.dialog_content);
//		VideoView mVideoView = (VideoView) findViewById(R.id.dialog_veidoView);
		
		
		if (dismiss)
			linearlayout.setOnClickListener(new dismissOnClickListener());
        
		
		
	}
	
	public void start(String path) {
		try {
			mMediaMusic = new MediaPlayer();
			mMediaMusic.setDataSource(path);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		play();
	}
	
	public void play(){
		if (mMediaMusic != null) {
			try {
				mMediaMusic.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
			mMediaMusic.start();  

		}
	}
	
	public void stop(View arg0){
		if(mMediaMusic!=null){  
			mMediaMusic.stop(); 
//			mMediaMusic.release();
		}
	}
	
	public void destroy() { 
		if(mMediaMusic!=null){  
			mMediaMusic.stop();
			mMediaMusic.release();
		}  
	}
}
